<template>
    <div class="register-container">
      <div class="register-box">
        <div class="register-header">
          <h2>注册新账号</h2>
        </div>
        
        <el-form :model="form" :rules="rules" ref="formRef">
          <el-form-item prop="username">
            <el-input v-model="form.username" placeholder="请输入用户名">
              <template #prefix>
                <el-icon><user /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input v-model="form.password" type="password" placeholder="请输入密码">
              <template #prefix>
                <el-icon><lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          
          <el-form-item prop="confirmPassword">
            <el-input v-model="form.confirmPassword" type="password" placeholder="请确认密码">
              <template #prefix>
                <el-icon><lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          
          <el-form-item prop="email">
            <el-input v-model="form.email" placeholder="请输入邮箱">
              <template #prefix>
                <el-icon><message /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          
          <el-form-item prop="phone">
            <el-input v-model="form.phone" placeholder="请输入手机号">
              <template #prefix>
                <el-icon><iphone /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          
          <el-form-item prop="code">
            <verify-code-input 
              v-model="form.code" 
              :phone="form.phone"
              @send-code="sendSmsCode"
            />
          </el-form-item>
          
          <el-form-item>
            <el-button type="primary" @click="handleRegister" :loading="loading">注册</el-button>
          </el-form-item>
        </el-form>
        
        <div class="register-footer">
          <el-link type="primary" @click="$router.push('/')">已有账号？立即登录</el-link>
        </div>
      </div>
    </div>
  </template>
  
  <script lang="ts" setup>
  import { ref } from 'vue'
  import { useRouter } from 'vue-router'
  import { User, Lock, Message, Iphone } from '@element-plus/icons-vue'
  import VerifyCodeInput from '@/components/VerifyCodeInput.vue'
  
  const router = useRouter()
  
  const form = ref({
    username: '',
    password: '',
    confirmPassword: '',
    email: '',
    phone: '',
    code: ''
  })
  
  const rules = {
    username: [
      { required: true, message: '请输入用户名', trigger: 'blur' },
      { min: 4, max: 16, message: '长度在4到16个字符', trigger: 'blur' }
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { min: 6, max: 20, message: '长度在6到20个字符', trigger: 'blur' }
    ],
    confirmPassword: [
      { required: true, message: '请确认密码', trigger: 'blur' },
      {
        validator: (rule: any, value: string, callback: any) => {
          if (value !== form.value.password) {
            callback(new Error('两次输入密码不一致'))
          } else {
            callback()
          }
        },
        trigger: 'blur'
      }
    ],
    email: [
      { required: true, message: '请输入邮箱', trigger: 'blur' },
      { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
    ],
    phone: [
      { required: true, message: '请输入手机号', trigger: 'blur' },
      { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
    ],
    code: [
      { required: true, message: '请输入验证码', trigger: 'blur' },
      { len: 6, message: '验证码为6位数字', trigger: 'blur' }
    ]
  }
  
  const loading = ref(false)
  
  const sendSmsCode = async () => {
    // 发送验证码逻辑
  }
  
  const handleRegister = async () => {
    try {
      loading.value = true
      // 注册逻辑
      // 注册成功后跳转到登录页
      router.push('/')
    } finally {
      loading.value = false
    }
  }
  </script>
  
  <style lang="scss" scoped>
  .register-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background: linear-gradient(135deg, #1a73e8, #0d47a1);
    
    .register-box {
      width: 400px;
      padding: 40px;
      background: #fff;
      border-radius: 8px;
      box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
      
      .register-header {
        text-align: center;
        margin-bottom: 30px;
        
        h2 {
          color: #1a73e8;
        }
      }
      
      .el-button {
        width: 100%;
      }
      
      .register-footer {
        text-align: center;
        margin-top: 20px;
      }
    }
  }
  </style>